package com.alien.admin.modules.utils;

import com.alibaba.fastjson.JSON;
import com.alien.admin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

import static com.alien.admin.modules.utils.YamlCicdTool.getCommonInfo;
import static com.alien.admin.modules.utils.YamlCicdTool.getServerInfoMap;

/**
 * Copyright (C), 2014-2021, 厦门神州鹰软件科技有限公司
 * FileName: YamlBuisnessService
 *
 * @author: zengzf
 * Date:     2021/10/9 13:38
 * Description:
 * History:
 */
@Slf4j
public class YamlBusinessService {

    public static String applicationDir = "APPLICATION_DIR";
    public static String applicationTarget = "APPLICATION_TARGET";

    public static String nginxUpstream = "NGINX_UPSTREAM";
    public static String nginxLocation = "NGINX_LOCATION";
    public static String nginxProxyPass = "NGINX_PROXY_PASS";

    public static String codeTypeFront = "front";

    /**
     * 推断某个项目的类型
     *
     * @param projectName
     * @return
     */
    public static String getCodeTypeByProjectName(String projectName, String env) {
        // 项目类型其实和环境没有关系，所以，我们这里直接写死。
        if (StringUtils.isBlank(env)) {
            env = "test";
        }
        Map<String, ServerInfo> serverInfoMap = getServerInfoMap(env);
        ServerInfo serverInfo = serverInfoMap.get(projectName);
        // 没有项目没有在配置文件，那么类型放空
        if (serverInfo == null) {
            return null;
        }
        // 如果项目有在项目文件中，但是type类型没配置，那么就是jar
        if (StringUtils.isBlank(serverInfo.getType())) {
            return "jar";
        }
        // front --> vue
        if (codeTypeFront.equals(serverInfo.getType())) {
            return "vue";
        }
        return serverInfo.getType();
    }

    /**
     * 获得某个项目的构建参数，项目类型为jar
     *
     * @param projectName
     * @return
     */
    public static String getBuildParamForVue(String projectName, String env) {

        Map<String, String> m = new HashMap<>(4);
        m.put(nginxUpstream, "");
        m.put(nginxLocation, "");
        m.put(nginxProxyPass, "");

        Map<String, ServerInfo> serverInfoMap = getServerInfoMap(env);
        ServerInfo serverInfo = serverInfoMap.get(projectName);
        if (serverInfo != null) {
            if (StringUtils.isNotBlank(serverInfo.getNginxUpstream())) {
                m.put(nginxUpstream, serverInfo.getNginxUpstream());
            }
            if (StringUtils.isNotBlank(serverInfo.getNginxLocation())) {
                m.put(nginxLocation, serverInfo.getNginxLocation());
            }
            if (StringUtils.isNotBlank(serverInfo.getNginxProxyPass())) {
                m.put(nginxProxyPass, serverInfo.getNginxProxyPass());
            }
        }
        // 如果服务没有配置，那么使用通用配置
        CommonInfo commonInfo = getCommonInfo(env);
        if (StringUtils.isBlank(m.get(nginxLocation))) {
            m.put(nginxLocation, commonInfo.getNginxLocation());
        }
        if (StringUtils.isBlank(m.get(nginxProxyPass))) {
            m.put(nginxProxyPass, commonInfo.getNginxProxyPass());
        }
        return JSON.toJSONString(m);
    }


    public static Map<String, String> getBuildParamMap() {
        Map<String, String> m = new HashMap<>(2);
        m.put(applicationDir, "");
        m.put(applicationTarget, "");
        return m;
    }

    /**
     * 获得某个项目的构建参数，项目类型为jar or tomcat
     *
     * @param projectName
     * @return
     */
    public static String getBuildParamForJar(String projectName, String env) {
        Map<String, String> m = getBuildParamMap();
        Map<String, ServerInfo> serverInfoMap = getServerInfoMap(env);
        ServerInfo serverInfo = serverInfoMap.get(projectName);
        if (serverInfo != null) {
            if (StringUtils.isNotBlank(serverInfo.getApplicationDir())) {
                m.put(applicationDir, serverInfo.getApplicationDir());
            }
            if (StringUtils.isNotBlank(serverInfo.getApplicationTarget())) {
                m.put(applicationTarget, serverInfo.getApplicationTarget());
            }
        }
        // 最差的情况下，APPLICATION_DIR使用通用的配置
        if (StringUtils.isBlank(m.get(applicationDir))) {
            CommonInfo commonInfo = getCommonInfo(env);
            m.put(applicationDir, commonInfo.getApplicationDir());
        }
        // APPLICATION_TARGET使用项目的名称
        if (StringUtils.isBlank(m.get(applicationTarget))) {
            m.put(applicationTarget, projectName);
        }
        return JSON.toJSONString(m);
    }

    /**
     * 获取某个项目的javaOpt值.
     *
     * 调用这个方法的时候，说明项目是需要javaOpt这个值的。
     * 优先使用项目自己配置的javaOpt值，如果没有，那么使用共用的值。
     *
     *
     * @param projectName
     * @param env
     * @return
     */
    public static String getJavaOptValue(String projectName, String env) {
        Map<String, ServerInfo> serverInfoMap = getServerInfoMap(env);
        ServerInfo serverInfo = serverInfoMap.get(projectName);
        if (serverInfo == null) {
            return null;
        }
        String javaOptValue = serverInfo.getJavaOpt();
        if(StringUtils.isBlank(javaOptValue)){
            CommonInfo commonInfo = getCommonInfo(env);
            javaOptValue = commonInfo.getJavaOpt();
        }
        return javaOptValue;
    }

    /**
     * 获得某个项目的构建参数。
     *
     * @param codeType
     * @param projectName
     * @return
     */
    public static String getBuildParamJsonStr(String codeType, String projectName, String env) {
        String buildParamJsonStr = null;
        if (codeType == null) {
            codeType = "codeTypeIsNull";
        }
        switch (codeType) {
            case "jar":
            case "tomcat":
                buildParamJsonStr = YamlBusinessService.getBuildParamForJar(projectName, env);
                break;
            case "vue":
                buildParamJsonStr = YamlBusinessService.getBuildParamForVue(projectName, env);
                break;
            default:
                log.info("unsupport type:{}, projectName:{}", codeType, projectName);
        }
        return buildParamJsonStr;
    }
}

